package com.jy.api.dao;

import com.jy.api.domain.NumCountData;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author xiaohua
 * @data 2022/12/6 0006 11:50
 */
public interface NumCountDataDao  extends JpaRepository<NumCountData, Integer>, JpaSpecificationExecutor<NumCountData> {

    NumCountData findTopByNumIdAndStatusOrderByIdDesc(int numId,int status);

    @Query(value = "SELECT COUNT( *) , DATE_FORMAT( `up_time`,  '%Y-%m-%d %H') AS upTime  FROM `num_count_data` WHERE `status` =0 AND `store_id` =? AND   `up_time` >=?  AND `up_time` <= ? GROUP BY upTime  ORDER BY `up_time` asc ", nativeQuery = true)
    List<Object[]> countByStoreIdAndAddTimeBetweenOrderByAddTimeAsc(String storeId, Date parse, Date parse1);

    @Query(value = "SELECT COUNT( *) FROM `num_count_data` WHERE `status` =0 AND `store_id` =? AND   `up_time` >=?  AND `up_time` <= ? ", nativeQuery = true)
     long countByStoreIdAndAddTimeBetween(String storeId, Date parse, Date parse1);

    List<NumCountData> findByDownTimeIsNotNull();

    NumCountData findTopByNumIdOrderByIdDesc(int numId);

    List<NumCountData> findByStoreIdAndDownTimeBetweenAndStatusOrderByDownTimeDesc(String storeId,Date beginTime,Date endTime,int status);

    @Transactional
    @Modifying
    void  deleteByDownTimeLessThanEqual(Date beginTime);

    List<NumCountData> findTop2ByNumIdAndStatusOrderByIdDesc(int id, int i);
}
